package com.ruoyi.APPMS.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.APPMS.domain.Logistics;
import com.ruoyi.APPMS.service.ILogisticsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流管理Controller
 * 
 * @author tianq
 * @date 2023-07-03
 */
@Api(tags = "物流信息管理(签收)")
@RestController
@RequestMapping("/logistics/logistics")
public class LogisticsController extends BaseController
{
    @Autowired
    private ILogisticsService logisticsService;

    /**
     * 查询物流管理列表
     */
    @ApiOperation("分页查询物流列表")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(Logistics logistics)
    {
        startPage();
        QueryWrapper<Logistics> queryWrapper = new QueryWrapper<>();
        if (logistics.getVehicleid()!= null && !"".equals(logistics.getVehicleid())){
            queryWrapper.like("vehicleid",logistics.getVehicleid());
        }
        if (logistics.getPartid()!= null && !"".equals(logistics.getPartid())){
            queryWrapper.like("partid",logistics.getPartid());
        }
        if (logistics.getCouriercompany()!= null && !"".equals(logistics.getCouriercompany())){
            queryWrapper.like("couriercompany",logistics.getCouriercompany());
        }
        if (logistics.getTrackingnumber()!= null && !"".equals(logistics.getTrackingnumber())){
            queryWrapper.like("trackingnumber",logistics.getTrackingnumber());
        }
        List<Logistics> list = logisticsService.list(queryWrapper);

        return getDataTable(list);
    }

    /**
     * 导出物流管理列表
     */
    @ApiOperation("导出选中的物流列表")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:export')")
    @Log(title = "物流管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Logistics logistics)
    {
        QueryWrapper<Logistics> queryWrapper = new QueryWrapper<>();
        if (logistics.getVehicleid()!= null && !"".equals(logistics.getVehicleid())){
            queryWrapper.like("vehicleid",logistics.getVehicleid());
        }
        if (logistics.getPartid()!= null && !"".equals(logistics.getPartid())){
            queryWrapper.like("partid",logistics.getPartid());
        }
        if (logistics.getCouriercompany()!= null && !"".equals(logistics.getCouriercompany())){
            queryWrapper.like("couriercompany",logistics.getCouriercompany());
        }
        if (logistics.getTrackingnumber()!= null && !"".equals(logistics.getTrackingnumber())){
            queryWrapper.like("trackingnumber",logistics.getTrackingnumber());
        }
        List<Logistics> list = logisticsService.list(queryWrapper);
        ExcelUtil<Logistics> util = new ExcelUtil<Logistics>(Logistics.class);
        util.exportExcel(response, list, "物流管理数据");
    }

    /**
     * 获取物流管理详细信息
     */
    @ApiOperation("获取物流管理详细信息")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:query')")
    @GetMapping(value = "/{logisticsid}")
    public AjaxResult getInfo(@PathVariable("logisticsid") Long logisticsid)
    {
        return success(logisticsService.getById(logisticsid));
    }

    /**
     * 新增物流管理
     */
    @ApiOperation("新增物流管理(不使用)")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:add')")
    @Log(title = "物流管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Logistics logistics)
    {
        return toAjax(logisticsService.save(logistics));
    }

    /**
     * 修改物流管理
     */
    @ApiOperation("修改物流管理(不使用)")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:edit')")
    @Log(title = "物流管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Logistics logistics)
    {
        return toAjax(logisticsService.updateById(logistics));
    }

    /**
     * 删除物流管理
     */
    @ApiOperation("删除物流管理")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:remove')")
    @Log(title = "物流管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logisticsids}")
    public AjaxResult remove(@PathVariable List<Long> logisticsids)
    {
        return toAjax(logisticsService.removeByIds(logisticsids));
    }

    //签收物流管理
    @ApiOperation("签收物流管理")
    @PreAuthorize("@ss.hasPermi('logistics:logistics:edit')")
    @PostMapping("/sign")
    public AjaxResult do_sign(@RequestBody Integer id_sign)
    {
        List<Logistics> to_sign = new ArrayList<>();
        to_sign.add(logisticsService.getById(id_sign));
//        System.out.println(logisticsService.do_sign(to_sign));
        return success(logisticsService.do_sign(to_sign));
    }
}
